package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView cookLv;
    //    数据源
    List<CookBean>mDatas;
    private CookAdapter adapter;
    DBOpenHelper helper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cookLv = findViewById(R.id.main_lv);
        mDatas = new ArrayList<>();
//        适配器
        adapter = new CookAdapter(this, mDatas);
        cookLv.setAdapter(adapter);

        loadDBData();
//      设置ListVIew的长按事件，长按可以删除数据
        cookLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                showDeleteDialog(position);
                return false;
            }
        });
//        设置ListView的短按事件，短按可以修改喜欢的数量
        cookLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CookBean bean = mDatas.get(position);
                int love = bean.getLove();
                int _id = bean.get_id();
                love++;  //点一下，就喜欢加一个
                bean.setLove(love);
                adapter.notifyDataSetChanged();

            }
        });
    }
    // 界面失去焦点时，会调用的方法
    @Override
    protected void onPause() {
        super.onPause();
        for (int i = 0; i < mDatas.size(); i++) {
            CookBean bean = mDatas.get(i);
            ContentValues values = new ContentValues();
            values.put("love",bean.getLove());
            db.update("cooktb",values,"_id=?",new String[]{bean.get_id()+""});
        }
        db.close();
    }

    // 显示删除对话框
    private void showDeleteDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示信息:").setMessage("是否要删除数据？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                            删除指定位置的数据
                deleteDataInPos(position);
            }
        });
        builder.setNegativeButton("取消",null);
        builder.create().show();
    }

    /*  删除数据库当中的数据，然后删除ListView当中的数据*/
    private void deleteDataInPos(int position) {
        CookBean cookBean = mDatas.get(position);
        mDatas.remove(cookBean);
        adapter.notifyDataSetChanged();
//        从数据库当中删除数据
        int id = cookBean.get_id();
//        删除的第一种方法，使用sql语句完成删除操作
//        String sql = "delete from cooktb where _id="+id;
//        db.execSQL(sql);

//        删除的第二种方法
        db.delete("cooktb","_id=?",new String[]{id+""});

    }

    /* 读取数据库当中的内容*/
    private void loadDBData() {
//       1. 获取数据库辅助管理者对象
        helper = new DBOpenHelper(this);
//        2.获取数据库对象
        db = helper.getWritableDatabase();
//        第一种查询方法
//        String sql = "select * from cooktb";
//        Cursor cursor = db.rawQuery(sql, null);
//       第二种查询方法
        Cursor cursor = db.query("cooktb", null, null,
                null, null, null, "love");
//        读取cursor当中的数据
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String food_str = cursor.getString(cursor.getColumnIndex("food_str"));
            String info = cursor.getString(cursor.getColumnIndex("info"));
            String pic = cursor.getString(cursor.getColumnIndex("pic"));
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            int love = cursor.getInt(cursor.getColumnIndex("love"));
            CookBean bean = new CookBean(id, name, food_str, info, love, pic);
            mDatas.add(bean);
        }
        cursor.close();
        adapter.notifyDataSetChanged();

    }


}